package demo01Singleton;

/**
 * @author: WangPeng
 * @createTime: 2022/11/08 14:02
 * @description:
 * @version: v1.0
 */
public class Test02Volatile {
    public static void main(String[] args) throws Exception{
        haha haha = new haha();
        haha.start();
        Thread.sleep(2000);
        System.out.println("这是main方法");
        haha.setFlag(false);
    }
}
class haha extends Thread{
    //volatile 可以保证多线程之间的可见性。
    private volatile boolean flag = true;

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        System.out.println("这是run方法");
        while (flag){

        }
    }
}
